window.onload=function(){
    let cartdiv=document.querySelector('.cartdiv');
    let cartobjarr=null;
    let uid=localStorage.getItem('id');
    loadcartlist()
    async function loadcartlist(){
      let res=await axios.get(cartlistAPI,{params:{id:uid}});
      cartobjarr=res.data.data;
    //   console.log(cartobjarr);
      updataulcartobjarray()
    }
    function updataulcartobjarray(){
        cartobjarr;
        let resarr=cartobjarr.map(pobj=>`
        <div class="cartdiv1">
              <h2><input type="checkbox" class="single-sel">${pobj.pname}<b>${pobj.pprice}</b></h2>
              <p class="p1">经验应届毕业生/本科</p>
              <p class="p2">${pobj.pdesc}</p>
              <img src="${pobj.pimg}">
              <ul>
                  <li>数媒<button class="btndel" data-pid="${pobj.pid}"><i class="layui-icon layui-icon-rate" style="font-size: 15px; color:orangered;" id="ccc"></i>取消收藏</button></li>
                  <li><p>企业服务，数据服务/北京</p></li>
              </ul>
          </div>
        `);
        cartdiv.innerHTML=resarr.join("");
        let btndels=document.querySelectorAll('.btndel');
        btndels.forEach((btn,i)=>{
            btn.onclick=async()=>{
                let pid=btn.getAttribute("data-pid");
                let {data}=await axios.get(cartdeleteAPI,{params:{pid,uid}});
                cartobjarr=cartobjarr.filter(v=>v.pid!=pid);
                updataulcartobjarray()
            }
        })
        // 单选
        let singlesels=document.querySelectorAll('.single-sel');
        singlesels.forEach((sel,i)=>{
            sel.onchange=function(){
                console.log(sel.checked)
                cartobjarr[i].checked=sel.checked;
                let allsel=cartobjarr.every(v=>v.checked==true);
                let allselIpt=document.querySelector('#all-sel');
                allselIpt.checked=allsel;
            }
        })
        // 全选按钮
        let selall=document.querySelector("#all-sel");
        selall.onchange=function(){
            singlesels.forEach(v=>v.checked=this.checked);
            cartobjarr.forEach(v=>v.checked=this.checked);
            let allsel=cartobjarr.every(v=>v.checked==true);
            let allselIpt=document.querySelector('#all-sel');
            allselIpt.checked=allsel;
        }
    }
}